//2019-9-5 OCCgeometry.js 是用于处理从云端发送回来的点位信息。





import {Group,BufferGeometry,MeshPhongMaterial,DoubleSide,Float32BufferAttribute,Mesh,LineBasicMaterial,Line} from "./three.module.js";



// 解析建模过程中的json文件信息。
// todo 待加入一些update局部更新信息
//:parameter
//       - JsonData 表示ajax回来后的json文件
// return
// ： mesh 表示进行封装处理能够被加载至scene的数据信息。
function OCCmodellingGeometry(url,scene ) {

    var json_data = (function () {
        var json = null;
        $.ajax({
            'async': false,
            'global': false,
            'url': url,
            'data':$(this).serialize(),     //暂时试了一下，加不加无所谓2019-9-10
            'dataType': "json",
            'success': function (data) {
                json = data;
            }
        });
        return json;
    })();

    //验证完毕，路径正确，引入成功
    // alert(json_data);
    // alert(url);

    var obj =json_data;    //将后端传过来的的json文件（字符串）转成JavaScript对象

    for (var n = 0, L = obj.dataBase.length; n < L; n++) {                          //根据database数组的长度作循环，依次加载物体
        for (var j = 0, leng = obj.dataBase[n].attributes.length; j < leng; j++) {  //根据attributes数组的长度作循环依次加载物体的面信息
            let shapeGeometry = new BufferGeometry();
            let testShape_material = new MeshPhongMaterial({
                specular: 0x333333,
                shininess: 0.9,
                side: DoubleSide,
                transparent: true,
                premultipliedAlpha: true,
                opacity: 1,
            });
            shapeGeometry.addAttribute("position", new Float32BufferAttribute(obj.dataBase[n].attributes[j].position.array, 3));//将点信息赋给geometry
            shapeGeometry.addAttribute('normal', new Float32BufferAttribute(obj.dataBase[n].attributes[j].normal.array, 3));//将法线数据赋给geometry
            testShape_material.color.fromArray(obj.dataBase[n].attributes[j].color.array);//读取每个面的颜色信息
            var mesh = new Mesh(shapeGeometry, testShape_material);
            Mesh.prototype.transId = null;//给THREE.Mesh添加transId属性
            mesh.castShadow = true;
            mesh.receiveShadow = true;
            mesh.transId = obj.dataBase[n].attributes[j].transId;//获取json中给定的transId
            scene.add(mesh);
            let material = new LineBasicMaterial({
                color: 0x000000,
                linewidth: 1,
                side: DoubleSide,
            });


            for (var i = 0, len = obj.dataBase[n].attributes[j].edgeline.array.length; i < len; i++) {//根据edgeline数组的长度作循环依次加载轮廓线
                var geometry = new BufferGeometry();
                geometry.addAttribute('position', new Float32BufferAttribute(obj.dataBase[n].attributes[j].edgeline.array[i], 3));//将轮廓线的点信息赋给lineGeometry
                var line = new Line(geometry, material);

                scene.add(line);
            }
        }
    }

    return mesh, line;
}



// 解析上传文件的json文件信息。
//:parameter
//       - occJson 表示ajax回来后的json文件
// return
// ： mesh 表示进行封装处理能够被加载至scene的数据信息。

function OCCfileGeometry(url,scene) {

    var json_data = (function () {
        var json = null;
        $.ajax({
            'async': false,
            'global': false,
            'url': url,
            'data':$(this).serialize(),     //暂时试了一下，加不加无所谓2019-9-10
            'dataType': "json",
            'success': function (data) {
                json = data;
            }
        });
        return json;
    })();

    //验证完毕，路径正确，引入成功
    // alert(json_data);
    // alert(url);

    var obj =json_data;    //将后端传过来的的json文件（字符串）转成JavaScript对象

    for (var n = 0, L = obj.dataBase.length; n < L; n++) {                          //根据database数组的长度作循环，依次加载物体
        for (var j = 0, leng = obj.dataBase[n].attributes.length; j < leng; j++) {  //根据attributes数组的长度作循环依次加载物体的面信息
            let shapeGeometry = new BufferGeometry();
            let testShape_material = new MeshPhongMaterial({
                specular: 0x333333,
                shininess: 0.9,
                side: DoubleSide,
                transparent: true,
                premultipliedAlpha: true,
                opacity: 1,
            });
            shapeGeometry.addAttribute("position", new Float32BufferAttribute(obj.dataBase[n].attributes[j].position.array, 3));//将点信息赋给geometry
            shapeGeometry.addAttribute('normal', new Float32BufferAttribute(obj.dataBase[n].attributes[j].normal.array, 3));//将法线数据赋给geometry
            testShape_material.color.fromArray(obj.dataBase[n].attributes[j].color.array);//读取每个面的颜色信息
            let mesh = new Mesh(shapeGeometry, testShape_material);
            Mesh.prototype.transId = null;//给THREE.Mesh添加transId属性
            mesh.castShadow = true;
            mesh.receiveShadow = true;
            mesh.transId = obj.dataBase[n].attributes[j].transId;//获取json中给定的transId
            scene.add(mesh);
            let material = new LineBasicMaterial({
                color: 0x000000,
                linewidth: 1,
                side: DoubleSide,
            });


            for (var i = 0, len = obj.dataBase[n].attributes[j].edgeline.array.length; i < len; i++) {//根据edgeline数组的长度作循环依次加载轮廓线
                var geometry = new BufferGeometry();
                geometry.addAttribute('position', new Float32BufferAttribute(obj.dataBase[n].attributes[j].edgeline.array[i], 3));//将轮廓线的点信息赋给lineGeometry
                var line = new Line(geometry, material);

                scene.add(line);
            }
        }
    }
}


export {OCCmodellingGeometry,OCCfileGeometry}